//过滤器
template.defaults.imports.theMonth = function(a) {
	let b = a.slice(5, 7);
	return b;
}
template.defaults.imports.theDay = function(a) {
	let b = a.slice(8, 10);
	return b;
}
template.defaults.imports.theTime = function(a) {
	let theSecond = (new Date().getTime() - new Date(a).getTime()) / 1000;
	let theYear = theSecond / (60 * 60 * 24 * 365);
	let theMonth = theSecond / (60 * 60 * 24 * 30);
	let theDay = theSecond / (60 * 60 * 24);
	let theHour = theSecond / (60 * 60);
	let theMinute = theSecond / 60;
	if (theYear >= 1) {
		return Math.floor(theYear) + '年';
	} else if (theMonth >= 1) {
		return Math.floor(theMonth) + '个月';
	} else if (theDay >= 1) {
		return Math.floor(theDay) + '天';
	} else if (theHour >= 1) {
		return Math.floor(theHour) + '小时';
	} else if (theMinute >= 1) {
		return Math.floor(theMinute) + '分钟';
	} else {
		return Math.floor(theSecond) + '秒';
	}
}

//蓝色日历牌
function blue(res) {
	let theDate;
	res.data.forEach((a, b) => {
		if (b = 0) {
			theDate = res.data[0].pub_date.slice(5, 10);
			a.MMDD = theDate;
		} else {
			if (a.pub_date.slice(5, 10) == theDate) {
				a.MMDD = null;
			} else {
				theDate = a.pub_date.slice(5, 10);
				a.MMDD = theDate;
			}
		}
	})
}
//请求articles
let count = 0;

function getArticles(fn, size) {
	$.ajax({
		type: 'get',
		url: '/api/articles',
		data: {
			pagenum: 1,
			pagesize: size
		},
		success: function(res) {
			if (res.status == 1) {
				console.log('失败');
			} else {
				fn(res);
				let theHTML = template('template-list', res);
				$('.kr_body').html(theHTML);

				if (res.data.length == count + add) {
					count = res.data.length
				} else {
					$('.kr_more>a').html('滑到底了');
				}
			}
		}
	})

}

//监测页面滚动距离
let theScroll = 0;
let timer = null;
$(document).scroll(() => {
	if (timer !== null) return;
	timer = setTimeout(function() {
		theScroll = $(document).scrollTop();
		timer = null;
	}, 200)
})

//刚打开页面 加载?条
let size = 7;
getArticles(blue, size);

//点击按钮 再加载?条
let add = 7
$(document).on('click', '.kr_more', function() {
	size = size + add;
	//记录当前滚动距离
	let nowScroll = theScroll;
	//加载
	getArticles(blue, size);
	//还原滚动距离
	$(document).scrollTop(nowScroll);
})

//合作伙伴
getLink();
